import * as Router from 'koa-router';
import dingTalkApi from 'dingtalk-app/dtalk/DingTalkApi';
import * as moment from 'moment';
import dTalkApiUtil from 'dingtalk-app/dtalk/dTalkApiUtil';
import approvalService from '../services/approvalService';
import visitMonthPlanService from '../services/visitMonthPlanService';
import visitMonthPlanModel from '../model/visitMonthPlanModel';
import consumerInfoService from '../services/consumerInfoService';
import storeInfoService from '../services/storeInfoService';
import storeInfoModel from '../model/storeInfoModel';
import consumerRecoderModel from '../model/consumerRecoderModel';
import { IRouterContext } from 'koa-router';
import dTalkApi from '../core/dTalkApi';

//员工拜访计划
let router = new Router();
router.prefix('/api/emp/monthplan');
//保存员工的临时计划
router.post('/saveTemporaryPlan', async (ctx: IRouterContext, next: any) => {
    let rq: any = ctx.request;
    let res = rq.body;
    let user: any = dingTalkApi.decode(ctx.header.authorization);
    let access_token=await dTalkApiUtil.getAccessToken();
    let rt = await visitMonthPlanService.saveTemporaryPlan(res, user);
    if (rt != null) {
        let dept=rt.deptId[0];
        let deptData=await dTalkApi.getDeptDetail(dept,access_token);
        let deptManagerUseridList: string = deptData.deptManagerUseridList || ''
        let num_= deptManagerUseridList.indexOf('|')
        // console.log(num_,'num_num_num_')
        let leaderIdLsit:any []=[]
        if(num_==(-1)){
            leaderIdLsit.push(deptManagerUseridList)
        }else{
            deptManagerUseridList = deptManagerUseridList.substr(0, deptManagerUseridList.length - 1)
            leaderIdLsit= deptManagerUseridList.split('|')
        }
        ctx.status = 200
        ctx.body = leaderIdLsit;
    } else {
        ctx.body = [];
    }
    await next()
});

//获取今日计划内容
router.post('/listMonthPlanByNowday', async (ctx: IRouterContext, next: any) => {
    let rq: any = ctx.request;
    let res = rq.body;
    let moment_ = moment().format("YYYY-MM-DD");
    let dayplanData = await visitMonthPlanService.listMonthPlanByNowday(res.userid, moment_);
    if (dayplanData.body.length>0) {
        ctx.body = dayplanData;
        ctx.status = 200;
    } else if (dayplanData.body.length == 0) {
        ctx.body = [];
    }
    await next()
});


//显示员工月拜访计划列表
router.post('/listMonthPlan', async (ctx: IRouterContext, next: any) => {
    let rq: any = ctx.request;
    let res = rq.body;
    // console.log(res);
    let monthPlanList = await visitMonthPlanService.listMonthPlan2(res.userid, res.month);
    if (monthPlanList.json_body.length != 0) {
        ctx.body = monthPlanList;
        ctx.status = 200;
    } else {
        ctx.body = null;
    }
    await next()
    //console.log(monthPlanList,'<----------------')
});


//执行取消操作的页面
//查看每日内容---针对是当月界面下的每日内容(有新计划)
router.post('/monthPlanDetail', async (ctx: IRouterContext, next: any) => {
    let rq: any = ctx.request;
    let res = rq.body;
    // console.log(res);
    let json = await visitMonthPlanService.listplanByDate(res.userid, res.date);
    if (json.data != null) {
        ctx.body = json;
        ctx.status = 200;
    } else {
        ctx.body = null;
    }
    await next()
});
//申请取消的计划只有临时计划
router.post('/monthPlanDetail2', async (ctx: IRouterContext, next: any) => {
    let rq: any = ctx.request;
    let res = rq.body;
    // console.log(res);
    let json = await visitMonthPlanService.listplanByDate2(res.userid, res.date);
    if (json.data != null) {
        ctx.body = json;
        ctx.status = 200;
    } else {
        ctx.body = null;
    }
    await next()
});

//当员工做申请取消操作时更新planStatus状态
//idList:数据的id列表  可以是多个id组成,也可以一个id
router.post('/updatePlanStatus', async (ctx: IRouterContext, next: any) => {
    let rq: any = ctx.request;
    let res = rq.body;
    await visitMonthPlanService.updatePlanStatus(res.idList);
    let json = await visitMonthPlanModel.findById({ _id: res.idList[0] });
    if (json != null && json.planStatus == '待审核') {
        ctx.body = '更新planStatus成功';
        ctx.status = 200;
    } else {
        ctx.body = '更新planStatus失败';
    }
    await next()
});


//查询审核结果(同意与不同意与驳回修改)的数据------每日计划(有新计划)页面
router.post('/approvalResult', async (ctx: IRouterContext, next: any) => {
    let rq: any = ctx.request;
    let res = rq.body;
    // console.log(res);
    let json = await approvalService.getapprovalResult(res.userid, res.month);
    // console.log(json);
    if (json.length != 0) {
        // console.log(json);
        ctx.body = json;
        ctx.status = 200;
    } else {
        ctx.body = null;
    }
    await next()
});

//查询审核结果(同意与不同意)详情------驳回修改预览页面
router.post('/approvalResultDetail', async (ctx: IRouterContext, next: any) => {
    let rq: any = ctx.request;
    let res = rq.body;
    let json = await approvalService.getApprovalResultDetail(res.id);
    if (json != null) {
        ctx.body = json;
        ctx.status = 200;
    } else {
        ctx.body = null;
    }
    await next()
});

//改接口是若员工修改了领导审核结果是不同意的计划,保存的该计划修改后的数据
router.post('/updateMonthplanAfterCheck', async (ctx: IRouterContext, next: any) => {
    let rq: any = ctx.request;
    let res = rq.body;
    let user: any = dingTalkApi.decode(ctx.header.authorization);
    // console.log(res,'res_res_res_res_res_res_res_',user)
    let access_token=await dTalkApiUtil.getAccessToken();
    let rt=await approvalService.updateMonthplanAfterCheck(res, user);
    // console.log(rt,'rt_rt_rt_rt_rt_rt_rt_rt_rt_rt_rt_rt_rt_rt_rt_')
    if (rt != null) {
        let dept=rt.deptId[0];
        let deptData=await dTalkApi.getDeptDetail(dept,access_token);
        let deptManagerUseridList: string = deptData.deptManagerUseridList || ''
        deptManagerUseridList = deptManagerUseridList.substr(1, deptManagerUseridList.length - 1)
        let leaderIdLsit:any = deptManagerUseridList.split('|')
        // console.log('11111111111111111',leaderIdLsit)
        ctx.status = 200
        ctx.body = leaderIdLsit;
    } else {
        ctx.body = [];
    }
    await next()
});

//该接口是根据id查询visitMonthPlanModel(负责页面跳转时部分内容的连带新页面显示)
router.post('/visitMonthPlanDataById', async (ctx: IRouterContext, next: any) => {
    let rq: any = ctx.request;
    let res = rq.body;
    let json = await visitMonthPlanService.getVisitMonthPlanDataById(res.id);
    if (json != null) {
        ctx.body = json;
        ctx.status = 200
    } else {
        ctx.body = null
    }
    await next()
});

//申请临时计划页面
//该接口是根据员工id查询所有客户信息
router.post('/getConsumerInfoByEmployeeId', async (ctx: IRouterContext, next: any) => {
    let rq: any = ctx.request;
    let res = rq.body;
    // console.log(res);
    let json = await consumerInfoService.getConsumerInfoByEmployeeId(res.userid);
    // console.log(json);
    if (json.length != 0) {
        ctx.body = json;
        ctx.status = 200
    } else {
        ctx.body = null
    }
    await next()
});
//该接口是根据客户名称获取客户信息
router.post('/getConsumerInfoByConsumerName', async (ctx: IRouterContext, next: any) => {
    let rq: any = ctx.request;
    let res = rq.body;
    let data = await consumerRecoderModel.findOne({ consumerName: res.consumerName, comment: 1 });
    if (data != null) {
        ctx.body = data;
        ctx.status = 200
    } else {
        ctx.body = null
    }
    await next()
});


//该接口是根据员工id查询门店信息
router.post('/getStoreInfoByEmployeeId', async (ctx: IRouterContext, next: any) => {
    let rq: any = ctx.request;
    let res = rq.body;
    // console.log(res);
    let json = await storeInfoService.checkStoreInfoByEmployeeId(res.userid);
    // console.log(json);
    if (json.length != 0) {
        ctx.body = json;
        ctx.status = 200
    } else {
        ctx.body = null
    }
    await next()
});


//该接口是根据门店名称查询门店信息
router.post('/getStoreInfoBystoreNmae', async (ctx: IRouterContext, next: any) => {
    // let res=ctx.request.body;
    let rq: any = ctx.request;
    let res = rq.body;
    // console.log(res);
    let json = await storeInfoModel.findOne({ storeName: res.storeName });
    // console.log(json);
    if (json != null) {
        ctx.body = json;
        ctx.status = 200
    } else {
        ctx.body = null;
    }
    await next()
});




import addNewService from '../services/addNewService';
//未测试
//该接口是用于当员工调整拜访计划的拜访日期
//dateList:[{id:'',planDate:''},{},{}]
// 0:调整成功   1:调整失败
router.post('/updatePlanDate', async (ctx: IRouterContext, next: any) => {
    let rq: any = ctx.request;
    let res = rq.body;
    console.log(res,'res_res_res_')
    let result=await addNewService.isAdjustMonthPlanPlanDate(res.dateList);
    if (result==0) {
        ctx.body = '更新拜访日期planDate成功';
        ctx.status = 200;
    } else {
        ctx.body = '更新拜访日期planDate失败';
    }
    await next()
});
//计划可以调整拜访日期
router.post('/monthPlanDetail3', async (ctx: IRouterContext, next: any) => {
    let rq: any = ctx.request;
    let res = rq.body;
    // console.log(res);
    let json = await visitMonthPlanService.listplanByDate(res.userid, res.date);
    if (json.data != null) {
        ctx.body = json;
        ctx.status = 200;
    } else {
        ctx.body = null;
    }
    await next()
});

//该接口是用于当员工调整拜访计划的拜访日期时更新planStatus状态---调整拜访日期操作
//idList:数据的id列表 [{id:'_id',date:'拜访日期'}]
router.post('/updatePlanStatus', async (ctx: IRouterContext, next: any) => {
    let rq: any = ctx.request;
    let res = rq.body;
    await visitMonthPlanService.updatePlanStatus2(res.idList);
    let json = await visitMonthPlanModel.findById({ _id: res.idList[0].id });
    if (json != null && json.planStatus == '待审核') {
        ctx.body = '调整成功';
        ctx.status = 200;
    } else {
        ctx.body = '调整失败';
    }
    await next()
});


export default router